require(['init'], function () {
  var arrlist=[];
  function Agencyinformation() {
    var $photo = $("#J_img");
    var $de_img=$("#de_img");

    $("input").focus(function(){
      $('#J_enroll_submit').attr("disabled",false);
    });
    $("textarea").focus(function(){
      $('#J_enroll_submit').attr("disabled",false);
    });
    $.ajax({
      url: '/api/organization/1',
      type: 'get',
      success: function (data) {
        $('#J_enroll_submit').attr("disabled","disabled");
        $('[name=orgname]').val(data.data.orgname);
        $('[name=orgaddress]').val(data.data.orgaddress);
        $('[name=orgcode]').val(data.data.orgcode);
        $('[name=businesslicense]').val(data.data.businesslicense);
        $('[name=legalperson]').val(data.data.legalperson);
        $('[name=companytel]').val(data.data.companytel);
        $('[name=SID]').val(data.data.SID);
        $('[name=SafeKey]').val(data.data.SafeKey);
        $('[name=eeourl]').val(data.data.eeourl);
        $('[name=registerDefaultPwd]').val(data.data.registerDefaultPwd);
        $('[name=eeocloudurl]').val(data.data.eeocloudurl);
        $('[name=courseminutes]').val(data.data.courseminutes);
        $('[name=agencyreferred]').val(data.data.agencyreferred);
        $('[name=email]').val(data.data.email);
        $('[name=icpcode]').val(data.data.icpcode);
        $photo.attr('src', data.data.agencylogo);
        $de_img.attr('src',data.data.defaultCourseImage);
      }
  })
    $("input").each(function(index,item){
      arrlist.push($(this).attr("name"))

    })

    $("#J_enroll_submit").on("click",function(){
        var orgname=$('[name=orgname]').val();
        var orgaddress=$('[name=orgaddress]').val();
        var orgcode=$('[name=orgcode]').val();
        var businesslicense=$('[name=businesslicense]').val();
        var legalperson=$('[name=legalperson]').val();
        var companytel=$('[name=companytel]').val();
        var SID=$('[name=SID]').val();
        var SafeKey=$('[name=SafeKey]').val();
        var eeourl=$('[name=eeourl]').val();
        var registerDefaultPwd=$('[name=registerDefaultPwd]').val();
        var eeocloudurl=$('[name=eeocloudurl]').val();
        var courseminutes=$('[name=courseminutes]').val();
        var agencyreferred=$('[name=agencyreferred]').val();
        var agencylogo = $('#J_uploadCover img').attr('src');
        var defaultCourseImage = $('#de_uploadCover img').attr('src');
        var email = $('[name=email]').val();
        var icpcode = $('[name=icpcode]').val();
        var arr={
        "orgname":orgname,
        "orgaddress":orgaddress,
        "orgcode":orgcode,
        "businesslicense":businesslicense,
        "legalperson":legalperson,
        "companytel":companytel,
        "SID":SID,
        "SafeKey":SafeKey,
        "eeourl":eeourl,
        "registerDefaultPwd":registerDefaultPwd,
        "eeocloudurl":eeocloudurl,
        "courseminutes":courseminutes,
        "agencyreferred":agencyreferred,
        "agencylogo":agencylogo,
        "defaultCourseImage":defaultCourseImage,
        "email":email,
        "icpcode":icpcode
        }
          if(registerDefaultPwd.length<6||registerDefaultPwd.length>16){
            $('input[name=registerDefaultPwd]').css({
              'border':'1px solid red'
            })
            $("#registerDefaultPwd").show();
            return false;
          }else{
            $('input[name=registerDefaultPwd]').css({
              'border':'1px solid #ccc'
            })
            $("#registerDefaultPwd").hide();
          }
        $('[name="SID"]').on('blur',function(){
            $("#SID").hide();
            $('input[name=SID]').css({
              'border':'1px solid #ccc'
            })
        })
        $('[name="SafeKey"]').on('blur',function(){
            $("#SECRET").hide();
            $('input[name=SafeKey]').css({
              'border':'1px solid #ccc'
            })
        })
        var popUpBox = new PopUpBox({
              type: 'dom',
              style: 'default contentScroll',
              submitBtnType: 'confirm',
              titleText:i18next.t('popupbox.warmprompt'),//温馨提示
              show: true,
              content: '<div style="margin-left:27px;margin-top:34px;">'+i18next.t('popupbox.oksave')+'</div>',//确定要保存吗?
              callback: {
                  yes: function (e) {
                    $.ajax({
                        url: '/api/organization/1',
                        data:arr,
                        type: 'put',
                        success:function(data){
                         $('#J_enroll_submit').attr("disabled",false);
                          var errlist=[];
                          for(var i=0,error=data.data;i<=error.length-1;i++){
                            errlist.push(error[i]);
                          }
                          if(errlist==""){
                              $("#SID").hide();
                              $('#SECRET').hide();
                              $('input').css({
                                'border':'1px solid #ccc'
                              })
                              window.location.href="homepage.html";
                          }

                          for(var j=0;j<=arrlist.length;j++){//name的值
                            for(var k=0;k<=errlist.length-1;k++){//错误的data
                              if(arrlist[j]==errlist[k]){
                                  $("#SID").show();
                                  $('#SECRET').show();
                                  $('input[name="'+errlist[k]+'"]').css({
                                    'border':'1px solid red'
                                })
                              }
                            }
                          }
                        }
                    })
                  }
              },
              position: {
                  width:448,
                  height:226
              }
          })

      })
   //图片上传
   //课程产品
   function imgUpload($container) {
       var $uploadCoverBox = $container.find('.icon-uploadimg')
       var $uploadCoverThumb = $uploadCoverBox.find('img')
       var $uploadInput = $uploadCoverBox.find('input')
       var coverUploadInput = new UploadInput({
           $dom: $uploadInput,
           done: function (uploadSrc) {
             console.log(uploadSrc);
             var uploadFullSrc='/api/file?u=' + uploadSrc;
             //var parseSrc = getFileNameByPath(uploadSrc);
             if (uploadSrc) {
               new ImgCropTool({
                 imgSrc: uploadFullSrc,
                 cropSize:[260, 175, 'courseCover'],
                 success:function(imgSrc){
                   var imgCropSrc = '/api/file?u=' + imgSrc;
                   $uploadCoverBox.attr('data-value', imgCropSrc);
                   $uploadCoverThumb.attr('src', imgCropSrc);
                 }
               })
             }
           }
       });
   }
   //LOGO
   function deimgUpload($container) {
       var $uploadCoverBox = $container.find('.icon-uploadimg')
       var $uploadCoverThumb = $uploadCoverBox.find('img')
       var $uploadInput = $uploadCoverBox.find('input')
       var coverUploadInput = new UploadInput({
           $dom: $uploadInput,
           done: function (deuploadSrc) {
             //console.log(uploadSrc);
             var deuploadFullSrc='/api/file?u=' + deuploadSrc;
             if (deuploadSrc) {
               new ImgCropTool({
                 imgSrc: deuploadFullSrc,
                 cropSize:[260, 90, 'courseCover'],
                 success:function(imgSrc){
                   var imgCropSrc = '/api/file?u=' + imgSrc;
                   $uploadCoverBox.attr('data-value', imgCropSrc);
                   $uploadCoverThumb.attr('src', imgCropSrc);
                 }
               })
             }
           }
       });
     }
       function getFileNameByPath(src) {
         return src.replace(/^.+\/([^\/]+)$/, function() {
           if(arguments[1]) {
             return arguments[1]
           }
           return ''
         });
       }

       imgUpload($('#J_imgupload'));
       deimgUpload($('#de_imgupload'));
      }
    Agencyinformation();


})
